//根据规格生成表格
function makeTable(){
	var spLength = $(".specification").length;//该商品有多少个规格 尺寸 颜色
	var isMake = true;
	var spvLengths = [];//商品规格下被选中的规格值数的数组
	//判断是否满足生成表格条件
	if(spLength > 0){   
		for(i = 0 ; i < spLength ; i ++ ){
			var spvLength = $("input[type='checkbox']:checked.spv_"+ ( i + 1 )).length;  //spv_1尺寸规格下选中值  长度
			spvLengths[i] = spvLength;
			if(spvLength <= 0){
				isMake = false;
			}
		}
	}else{
		$("#chooseSpecification").hide();
		isMake = false;
	}
	//生产表格
	if(isMake){
		$("#price").attr("readonly","readonly");
		$("#stock").attr("readonly","readonly");
		var tableHTML = "";//表格代码
		var tableRowLength = 1;//表格行数
		var rowspanNum = [];//每一列rowspan数数组
		//计算表格行数
		for(i = 0 ; i < spLength ; i ++){
			tableRowLength *= spvLengths[i];
		}
		//计算每一列rowspan数
		var newRowspanNum = 1;
		for(i = 0 ; i < spLength ; i ++){
			newRowspanNum *= spvLengths[i];
			rowspanNum[i] = tableRowLength/newRowspanNum;
		} 
		tableHTML += "<table width='100%'>";
		for(i = 0 ; i < tableRowLength ; i ++){
			var rowNum = i + 1;//当前行数
			var columnCount = spLength;//当前行列数
			//计算当前行列数
			/* alert("第"+rowNum+"行：共"+spLength+"列"); */
			for(x = 0 ; x < spLength ; x ++){
				/* alert("第"+(x+1)+"列，rowspanNum:"+rowspanNum[x]+"取余："+rowNum % rowspanNum[x]); */
				if(rowNum % rowspanNum[x] != 1 && rowspanNum[x] != 1){
					columnCount --;
				}
			}
			tableHTML += "<tr width='100%'>";
			//动态生成<td>
			for(j = (spLength - columnCount) ; j < spLength ; j ++){
				tableHTML += "<td width='"+((100-20)/(spLength+2))*0.6+"%' rowspan='"+rowspanNum[j]+"'>"+getHTML(rowNum,(j+1),rowspanNum)+"</td>";
			}
			tableHTML += 	"<td width='"+((100-20)/(spLength+2))*1.2+"%'>" + 
								getValues(rowNum,rowspanNum) + 
								"<span style='float:left'>请输入价格:</span> " +
								"<input class='tablePrice' id='price_"+rowNum+"' " +
										"style='float:left;width:80px;' type='text' " +
										"name='tablePrice' value='' onblur='checkTable("+rowNum+")'>" +
							"</td>";
			tableHTML += 	"<td width='"+((100-20)/(spLength+2))*1.2+"%'>" +
								"<span style='float:left'>请输入库存:</span> " +
								"<input class='tableStock' id='stock_"+rowNum+"' " +
										"style='float:left;width:80px;' type='text' " +
										"name='tableStock' value='' onblur='checkTable("+rowNum+")'>" +
								"<input class='tablePSMIds' name='tablePSMIds' value='' type='hidden'>" +
							"</td>";
			tableHTML += 	"<td width='20%'>" +
								"<span class='fullSKU'></span><span id='checkTable_"+rowNum+"' style='color:red;'>*</span></td>";
			tableHTML += 	"</tr>";
		}
		
		tableHTML += "<tr><td><input type='hidden' id='specificationCount' value='"+tableRowLength+"'></td></tr>";
		tableHTML += "</table>";
		$("#specification_table").html(tableHTML);
		$("#myText").val(tableHTML);
		$("#myDisplay").show();
	}else{
		$("#myDisplay").hide();
		$("#specification_table").html("");
	}
	setTableValue();
}
//生成表格内的规格及规格值名称
function getHTML(row,column,rowspanNum){
	var specification = $(".specification").eq(column-1).val();//当前列的规格名
	var j = $("input[type='checkbox']:checked.spv_"+ column).length;//当前列有多少不重复元素
	var num = Math.ceil(row / rowspanNum[column-1]);
	if(num % j == 0){num = j;}else{num = num % j;}
	var values = $("input[type='checkbox']:checked.spv_"+column).eq(num-1).attr("spvname");
	return specification + ":" + values;
}
//计算规格及规格值id
function getValues(row,rowspanNum){
	var spLength = $(".specification").length;//当前有多少规格
	var str = "";
	var strId = "";
	var strName = "";
	for(column = 1 ; column <= spLength ; column ++){
		var specification = $(".specification").eq(column-1).val();//当前列的规格名
		var specificationValue = $(".specification").eq(column-1).attr("spname");//当前列的规格名
		var j = $("input[type='checkbox']:checked.spv_"+ column).length;//当前列有多少不重复元素
		var num = Math.ceil(row / rowspanNum[column-1]);
		if(num % j == 0){num = j;}else{num = num % j;}
		var value = $("input[type='checkbox']:checked.spv_"+column).eq(num-1).val();//规格值id
		var name = $("input[type='checkbox']:checked.spv_"+column).eq(num-1).attr("spvname"); //规格值名称
		strId += specificationValue + ":" + value + ",";
		strName += specification + ":" + name + ",";
	}
	strId = strId.substring(0,strId.length - 1);
	strName = strName.substring(0,strName.length - 1);
	str += "<input type='hidden' name='specificationValuesId' value='" + strId + "'>";//id
	str += "<input type='hidden' name='specificationValuesName' value='" + strName + "'>";//name
	return str;
}
function setTableValue(){
	var _productNo = $("#productNo").val();
	$(".psmList").each(function(){
		var _psm = $(this);
		var _psmSVN = _psm.attr("specificationValuesName");
		var _psmPrice = _psm.attr("price");
		var _psmStock = _psm.attr("stock");
		var _sku = "SKU:" + _productNo + _psm.attr("orders");
		$("#myDisplay").find("input[name='specificationValuesName']").each(function(){
			var _table = $(this);
			var _tableSVN = _table.val();
			if(_tableSVN == _psmSVN){
				_table.siblings("input[name='tablePrice']").val(_psmPrice);
				_table.parent().next().find("input[name='tableStock']").val(_psmStock);
				_table.parent().next().next().find(".fullSKU").html(_sku);
			}
		});
	});
}


//生成属性及属性值字段
function changeAttributeValues(){
	var attributeLength = $(".attribute").length;// 属性个数
	if(attributeLength > 0){// 有属性
		// var i = 1;
		$(".AVAttribute").each(function(){// 属性值
			var strId = "";
			var strName = "";
			$(this).find("input:checked.to_check").each(function(){
				strId += $(this).val() + ",";
			});
			$(this).find("input:checked.to_check_name").each(function(){
				strName += $(this).val() + ",";
			});
			strId = strId.substring(0,strId.length-1);
			strName = strName.substring(0,strName.length-1);
			// alert("第"+i+"个strId:"+strId);
			$(this).find(".product_attributeValues_id").val(strId);
			$(this).find(".product_attributeValues_name").val(strName);
			// i ++;
		});
	}else{
		$("#chooseAttribute").hide();
	}
}
